<h1><?= strtotitle(__('members')) ?></h1>
<p>
    <?= $this->Html->link("+" . __('new') . " " . __('member'), array('action'=>'create')) ?> |
    <?= $this->Html->link(__('Print'), array('action' => 'stampa'), array('onclick' => 'return popUp(this)')) ?>
</p>

<?= $this->Form->create('Filter', array(
    'url'   => '/members',
    'class' => 'filter',
    'style' => 'position:relative'
)) ?>
    
    <?php
        $display   = 'none';
        $arrowHtml = array('▼', '▲');
        $arrow     = $arrowHtml[0];
        if (Member::isAdvancedSearch($filter)) {
            $display = 'block';
            $arrow   = $arrowHtml[1];
        }
    
    ?>
    
    <?= $this->Form->text('keyword', array('value'=>@$filter['keyword'], 'class'=>'large')) ?>
       
    <?= $this->Html->link($arrow, 'javascript:void(0)', array(
        'class'   => 'left arrow',
        'onclick' => 'toggleAdvancedSearch()',
        'title'   => __('Advanced search')
    )) ?>
    <?= $this->Form->submit(__('Search'), array('div' => false, 'style' => 'clear:none;float:left')) ?>
    <?= $this->Html->link(__('Cancel'), '/members', array(
        'class'   => 'left',
        'style'   => 'margin:5px 10px'
    )) ?>
    
    <div id="advanced-search" style="display:<?= $display ?>;padding:10px 0;clear:both">
        <?= $this->App->input('name', 'text', @$filter['name'], array(
            'class' => 'large',
            'label' => __('firstname') . ', ' . __('surname')
        )) ?>
        
        <?= $this->Form->label('Filter.registration_date.from', strtotitle(strtotitle(__('registration date'))) . ':') ?>
        <?= $this->Form->text('Filter.registration_date.from', array('value' => @$filter['registration_date']['from'], 'class'=>'datepicker')) ?>
        <?= $this->Form->label('Filter.registration_date.to', ' - ', array('class'=>'inline auto')) ?>
        <?= $this->Form->text('Filter.registration_date.to', array('value' => @$filter['registration_date']['to'], 'class'=>'datepicker')) ?>
        
        <?= $this->App->input('category', 'select', @$filter['category'], array('options' => $categories, 'empty'=>'')) ?>
		<?= $this->App->input('voice', 'select', @$filter['voice'], array(
	        'options' => $voices,
	        'label'   => 'voice',
	        'empty'   => '',
	        'labelOptions' => array( 'class' => 'inline auto', 'style'=>'margin-left:20px')
		)) ?>
        
        <?= $this->Form->label("_", strtotitle(__('choir')) . ":") ?>
		<div class="left" style="margin-left:10px">
			<?= $this->Form->checkbox('choir_sex_mixed', array('value' => 1, 'checked'=>(int)@$filter['choir_sex_mixed']>0)) ?>
			<?= $this->Form->label('choir_sex_mixed', __('mixed'), array(
		        'class' => 'inline auto',
		        'style' => 'margin-right:15px'
			)) ?>
			
			<?= $this->Form->checkbox('choir_sex_male', array('value' => 1, 'checked'=>(int)@$filter['choir_sex_male']>0)) ?>
			<?= $this->Form->label('choir_sex_male', __('male'), array(
		        'class'=>'inline auto',
		        'style' => 'margin-right:15px'
			)) ?>
			
			<?= $this->Form->checkbox('choir_sex_female', array('value' => 1, 'checked'=>(int)@$filter['choir_sex_female']>0)) ?>
			<?= $this->Form->label('choir_sex_female', __('female'), array(
		        'class'=>'inline auto'
			)) ?>
		</div>
		
		<?= $this->App->input('function', 'select', @$filter['function'], array('options' => $functions, 'empty'=>'')) ?>
        <?= $this->App->input('education', 'select', @$filter['education'], array(
            'options'      => $educations,
            'labelOptions' => array( 'class' => 'inline auto', 'style'=>'margin-left:20px'),
            'empty'        => '' 
        )) ?>
		
		<?= $this->Form->label('Filter.birthday.from', strtotitle(strtotitle(__('birthday'))) . ':') ?>
        <?= $this->Form->text('Filter.birthday.from', array('value' => @$filter['birthday']['from'], 'class'=>'datepicker')) ?>
        <?= $this->Form->label('Filter.birthday.to', ' - ', array('class'=>'inline auto')) ?>
        <?= $this->Form->text('Filter.birthday.to', array('value' => @$filter['birthday']['to'], 'class'=>'datepicker')) ?>
        
        <?= $this->Form->label('passport_number', strtotitle(__('passport number')).':') ?>
        <?= $this->Form->select('passport_number', array(
                -1 => __('don\'t have'),
                1 => __('have')
        ), array(
            'value' => @$filter['passport_number']
        )) ?>
		<?= $this->App->input('passport_country', 'text', @$filter['passport_country'], array(
	        'labelOptions' => array( 'class' => 'inline auto', 'style'=>'margin-left:20px')
		)) ?>
        
    </div>

<?= $this->Form->end() ?>

<? if(count($members)): ?>
<table>
	<tr>
		<th><?= __('name') ?></th>
		<th width="2%"><?= __('choir') ?></th>
		<th width="10%"><?= __('voice') ?></th>
		<th><?= __('functions') ?></th>
		<th width="1%"></th>
	</tr>
	<? foreach($members as $member): ?>
	<tr>
		<td><?= $this->Html->link($member['Member']['firstname'] . " " . $member['Member']['surname'], array('action' => 'edit', $member['Member']['id'])) ?></td>
		<td>
			<?
				$choirs = array();
				if ($member['Member']['choir_sex_mixed']) $choirs[] = __('mixed');
				if ($member['Member']['choir_sex_male']) $choirs[] = __('male');
				if ($member['Member']['choir_sex_female']) $choirs[] = __('female');
				
				echo implode(", ", $choirs);
			?>
		</td>
		<td><?= __($member['Member']['voice']) ?></td>
		<td>
			<? 
				$member_functions = empty($member["MemberChoirFunction"]) ? array() : Set::extract("{n}.Function.name", $member["MemberChoirFunction"]);
				foreach($member_functions as &$mf) {
					$mf = __($mf);
				}
			?>
			<?= empty($member_functions) ? "" : implode("<br>", $member_functions) ?>
		</td>
		<td>
			<?= $this->Html->link(__('delete'), array('action'=>'delete', $member['Member']['id']), array('onclick'=>"return confirm('".__("Do you want to delete member?")."')")) ?>
		</td>
	</tr>
	<? endforeach; ?>
</table>
<?= $this->element("pagination", array(
	"total" => $total,
	"page" => $page
)) ?>
<? else: ?>
<br clear="all">
<p><?= __('no records') ?>.</p>
<? endif; ?>

<script type="text/javascript">
function toggleAdvancedSearch() {
	var box   = $('#advanced-search');
	var arrow = $('a.arrow');
	var speed = 500;
	
	if (box.is(':visible')) {
		box.slideUp(speed);
		arrow.removeClass('opened');
		arrow.text('<?= $arrowHtml[0] ?>');
	} else {
		box.slideDown(speed);
		arrow.addClass('opened');
		arrow.text('<?= $arrowHtml[1] ?>');
	}
}
function popUp(a) {
	a = $(a);
	var url = a.attr('href');
	var win = window.open(url, 'print', 'width=800, height=600, scrollbars=yes');
	win.focus

	return false;
}
</script>